package com.dormitory_springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.dormitory_springboot.bean.Apply;
import com.dormitory_springboot.mapper.ApplyMapper;
import com.dormitory_springboot.service.ApplyService;
import com.dormitory_springboot.vo.ResultV1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @auther 夏杨
 **/
@RestController
@RequestMapping("/apply")
public class ApplyController {
    @Autowired
    ApplyService applyService;

    @GetMapping("/getApplyBySid/{sid}")
    public Apply getApplyBySid(@PathVariable("sid")String sid){
        AtomicBoolean canApply = new AtomicBoolean(true);
        QueryWrapper<Apply> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sid",sid);
        queryWrapper.eq("issolved",false);
        return applyService.getOne(queryWrapper);
    }
    @GetMapping("/getUsefulApplyBySex/{sex}")
    public List<ResultV1> getUsefulApplyBySex(@PathVariable("sex")String sex){
        if(sex.equals("男"))
            return applyService.getUsefulApplyBySex("2%");
        else return applyService.getUsefulApplyBySex("1%");
    }
    @GetMapping("/addApply")
    public void addApply(Apply apply){
        applyService.save(apply);
    }
    @GetMapping("/accept/{id}/{hmid}")
    public void accept(@PathVariable("id")int id,@PathVariable("hmid")String hmid){
        UpdateWrapper<Apply> updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",id).set("hmid",hmid);
        applyService.update(updateWrapper);
    }
    @GetMapping("/updata")
    public void updata(Apply apply){
        UpdateWrapper<Apply> updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",apply.getId());
        updateWrapper.set("type",apply.getType());
        updateWrapper.set("infor",apply.getInfor());
        applyService.update(updateWrapper);
    }
    @GetMapping("/solvedById/{id}")
    public void solvedBySid(@PathVariable("id")String id){
        UpdateWrapper updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        updateWrapper.set("issolved",true);
        applyService.update(updateWrapper);
    }
}
